//
//  RecmdListCtrl.m
//  laowu
//
//  Created by 尹桥印 on 15/6/3.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "RecmdListCtrl.h"
#import "MeDataTool.h"
#import "RecmtItemCell.h"
#import "DatePickerView.h"

@interface RecmdListCtrl ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,DatePickerViewDelegate>
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;

@property (weak, nonatomic) IBOutlet UITextField *startField;

@property (weak, nonatomic) IBOutlet UITextField *endField;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@property (nonatomic, strong) NSMutableArray *itemArray;

@end

@implementation RecmdListCtrl

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        self.itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:RGBColor(240, 240, 240)];
    self.title = @"我的推荐人";
    [self requestDataWithKeyword:@"" begin:@"" end:@""];
    [self.tableView setTableFooterView:[UIView new]];
}

-(void)requestDataWithKeyword:(NSString *)keyword begin:(NSString *)begin end:(NSString *)end{
//    MBProgressHUD *hud = [DialogUtils showLoadingWithView:nil];
    [MeDataTool getRecmtListWithUserName:keyword begin:begin end:end success:^(RecmdRes *result) {
//        [hud hide:YES];
        if (result.isSucc) {
            [self.itemArray removeAllObjects];
            [self.itemArray addObjectsFromArray:result.datas];
            [self.tableView reloadData];
            
            [self.countLabel setText:[NSString stringWithFormat:@"总共推荐%ld人",result.datas.count]];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
//        [hud hide:YES];
        [DialogUtils showTipsWithView:nil String:@"加载失败"];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecmtItemCell *cell = [RecmtItemCell itemWithTabelView:tableView];
    [cell setItemInfo:self.itemArray[indexPath.row]];
    return cell;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self requestDataWithKeyword:searchText begin:self.startField.text end:self.endField.text];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.searchBar endEditing:YES];
}

- (IBAction)leftBtnClick:(UIButton *)sender {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    DatePickerView *view = [DatePickerView datepicker];
    view.delegate = self;
    view.resultType = DatePickeResultType2;
    view.view = self.startField;
    [view setFrame:window.frame];
    [window addSubview:view];
}

- (IBAction)rightBtnClick:(UIButton *)sender {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    DatePickerView *view = [DatePickerView datepicker];
    view.delegate = self;
    view.resultType = DatePickeResultType2;
    view.view = self.endField;
    [view setFrame:window.frame];
    [window addSubview:view];
}

-(void)datePickerValueChange:(DatePickerView *)pickerView resultStr:(NSString *)dateStr{
    UITextField *button = (UITextField *)pickerView.view;
    button.text = dateStr;
}

- (IBAction)tap:(id)sender {
    [self.view endEditing:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}


@end
